package com.tepth.vehiclemaintenance.adapter;

import android.content.Context;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tepth.vehiclemaintenance.R;
import com.tepth.vehiclemaintenance.bean.MaintainListWork;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangyu on 2017/5/12.
 */

public class WorkItemAdapter extends BaseQuickAdapter<MaintainListWork.ListContentsBean, BaseViewHolder> {
    private VectorDrawableCompat mDrawableCompatNormal;
    private VectorDrawableCompat mDrawableCompatCancel;
    private VectorDrawableCompat mDrawableCompatComplete;

    public WorkItemAdapter(Context context, int layoutResId, List<MaintainListWork.ListContentsBean> data) {
        super(layoutResId, data);
        mDrawableCompatNormal = VectorDrawableCompat.create(context.getResources(),
                R.drawable.ic_check_circle_gray_24dp, context.getTheme());
        mDrawableCompatCancel = VectorDrawableCompat.create(context.getResources(),
                R.drawable.ic_check_circle_red_24dp, context.getTheme());
        mDrawableCompatComplete = VectorDrawableCompat.create(context.getResources(),
                R.drawable.ic_check_circle_green_24dp, context.getTheme());
    }

    @Override
    protected void convert(BaseViewHolder helper, MaintainListWork.ListContentsBean item) {
        String des = item.getContentDesc();
        helper.setText(R.id.tv_content_des, des == null ? "" : des);
        View layoutOperation = helper.getView(R.id.layout_operation);
        switch (item.getState()) {
            case 0://正常
                helper.setImageDrawable(R.id.iv_check_state, mDrawableCompatNormal);
                layoutOperation.setVisibility(View.GONE);
                break;
            case 1://已打卡
                helper.setImageDrawable(R.id.iv_check_state, mDrawableCompatComplete);
                layoutOperation.setVisibility(View.VISIBLE);
                setOperation(helper, item);
                break;
            case 2://待打卡
                layoutOperation.setVisibility(View.VISIBLE);
                helper.setImageDrawable(R.id.iv_check_state, mDrawableCompatCancel);
                setOperation(helper, item);
                break;

        }

        helper.addOnClickListener(R.id.iv_check_state);
        helper.addOnClickListener(R.id.layout_content);
        helper.addOnClickListener(R.id.iv_goto);
    }

    private void setOperation(BaseViewHolder helper, MaintainListWork.ListContentsBean item) {
        String personName = item.getWorkPersonName();
        String operationContent = "";
        View layoutOperation = helper.getView(R.id.layout_operation);
        List<MaintainListWork.ListContentsBean.ListRemarkBean> listTroubleBean =
                item.getListRemark();
        if (listTroubleBean != null) {
            List<MaintainListWork.ListContentsBean.ListRemarkBean> tempList =
                    new ArrayList<>();
            for (MaintainListWork.ListContentsBean.ListRemarkBean bean : listTroubleBean) {
                if (bean.isChecked()) {
                    tempList.add(bean);
                }
            }
            operationContent += assemblyData(tempList);
        }

        if (operationContent.isEmpty()) {
            layoutOperation.setVisibility(View.GONE);
        } else {
            operationContent = operationContent.substring(0, operationContent.length() - 1);
            layoutOperation.setVisibility(View.VISIBLE);
            helper.setText(R.id.tv_content_operator, operationContent);
            if (personName != null && !personName.isEmpty() && "null".equals(personName)) {
                helper.setText(R.id.tv_person_name, personName);
            } else {
                helper.setText(R.id.tv_person_name, "无");
            }
        }


    }

    private String assemblyData(List<MaintainListWork.ListContentsBean.ListRemarkBean> listRemarkBeen) {
        String operationContent = "";
        for (MaintainListWork.ListContentsBean.ListRemarkBean bean : listRemarkBeen) {
            MaintainListWork.ListContentsBean.ListRemarkBean.OperationBean operationBean =
                    bean.getOperation();
            if (operationBean != null) {
                operationContent += operationBean.getName();
            }
            MaintainListWork.ListContentsBean.ListRemarkBean.AccessoryBean accessoryBean =
                    bean.getAccessory();
            MaintainListWork.ListContentsBean.ListRemarkBean.AccessoryBean componentBean =
                    bean.getComponent();
            if (accessoryBean != null) {
                operationContent += accessoryBean.getName();
                operationContent += bean.getUseCount();
                operationContent += accessoryBean.getUnit();
            } else {
                if (componentBean != null) {
                    operationContent += componentBean.getName();
                    operationContent += bean.getUseCount();
                    operationContent += componentBean.getUnit();
                }
            }
            operationContent += "\n";
        }
        return operationContent;
    }
}
